package com.ovt.doorbell.payment.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ovt.doorbell.payment.common.exception.OVTRuntimeException;
import com.ovt.doorbell.payment.dao.ResourceDao;
import com.ovt.doorbell.payment.dao.constant.ResourceType;
import com.ovt.doorbell.payment.dao.vo.PageInfo;
import com.ovt.doorbell.payment.dao.vo.Resource;
import com.ovt.doorbell.payment.dao.vo.UsedResource;
import com.ovt.doorbell.payment.service.exception.ServiceErrorCode;
import com.ovt.doorbell.payment.service.exception.ServiceException;

@Service
public class ResourceServiceImpl implements ResourceService
{
    @Autowired
    private ResourceDao resourceDao;
    
    @Override
    public float getPrice(int resId) throws ServiceException
    {
        float price = 0;
        try
        {
            price = resourceDao.getPrice(resId);
        }
        catch (OVTRuntimeException e)
        {
            throw new ServiceException(ServiceErrorCode.DB,
                    "Failed to get price by resource Id!", e);
        }
        return price;
    }

    @Override
    public void addResource(Resource resource) throws ServiceException
    {
        try
        {
            resourceDao.save(resource);
        }
        catch (OVTRuntimeException e)
        {
            throw new ServiceException(ServiceErrorCode.DB,
                    "Failed to save resource!", e);
        }
    }

    @Override
    public void deleteResource(int resId) throws ServiceException
    {
        try
        {
            resourceDao.delete(resId);
        }
        catch (OVTRuntimeException e)
        {
            throw new ServiceException(ServiceErrorCode.DB,
                    "Failed to delete resource!", e);
        }
    }

    @Override
    public void updateResource(Resource resource) throws ServiceException
    {
        try
        {
            resourceDao.update(resource);
        }
        catch (OVTRuntimeException e)
        {
            throw new ServiceException(ServiceErrorCode.DB,
                    "Failed to update resource!", e);
        }
    }

    @Override
    public List<Resource> getAllResources(PageInfo page) throws ServiceException
    {
        List<Resource> resources = null;
        try
        {
            resources = resourceDao.getResourceList(page);
        }
        catch (OVTRuntimeException e)
        {
            throw new ServiceException(ServiceErrorCode.DB,
                    "Failed to get resource list!", e);
        }
        return resources;
    }

    @Override
    public Resource getResource(ResourceType type, int size) throws ServiceException
    {
        Resource resource = null;
        
        try
        {
            resource = resourceDao.getResource(type, size);
        }
        catch (OVTRuntimeException e)
        {
            throw new ServiceException(ServiceErrorCode.DB,
                    "Failed to get resource with type and size!", e);
        }
        
        return resource;
    }

    @Override
    public UsedResource getUsedResource(long usedId) throws ServiceException
    {
        return null;
    }

    @Override
    public Resource getResource(String resourceCode) throws ServiceException
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Resource> getDefaultResource() throws ServiceException
    {
        // TODO Auto-generated method stub
        return null;
    }
    
    
    
}
